package com.hornsun.manage.slideshow.api;

import com.hornsun.domain.dbo.Slideshow;
import com.hornsun.manage.slideshow.dto.SlideshowDTO;
import com.hornsun.repository.SlideshowRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by 编译中... on 2018/5/8.
 */
@RestController
@RequestMapping("/manage/slideshow")
public class SlideshowApi {
    @Resource
    private SlideshowRepository slideshowRepository;

    @GetMapping("/getSlideshow")
    public ResponseEntity<List<Slideshow>> getSlideshow(){
        return new ResponseEntity<List<Slideshow>>(slideshowRepository.selectSlideshow(),HttpStatus.OK);
    }

    @Transactional
    @PostMapping("/update")
    public ResponseEntity<?> updateSlideshow(@RequestBody SlideshowDTO[] slideshowDTOS){
        slideshowRepository.deleteSlideshow();
        if (slideshowDTOS.length>0) {
            for (int i = 0; i < slideshowDTOS.length; i++) {
                Slideshow slideshow = new Slideshow();
                slideshow.setId(UUID.randomUUID().toString());
                slideshow.setImage(slideshowDTOS[i].getImage());
                slideshow.setDesciption(slideshowDTOS[i].getDesciption());
                slideshow.setType(slideshowDTOS[i].getType());
                slideshow.setContent(slideshowDTOS[i].getContent());
                slideshow.setSequence(i);
                slideshow.setUpdateTime(new Date());
                slideshowRepository.insertSlideshow(slideshow);
            }
        }
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    @GetMapping("/getByGuid")
    public ResponseEntity<?> getByGuid(@RequestParam("guid")String slideshowGuid){
        return new ResponseEntity<Object>(slideshowRepository.getByGuid(slideshowGuid),HttpStatus.OK);
    }


}
